<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>
<%@page import="com.feib.soeasy.model.User"%>

<script type="text/javascript">
<!--
	$(document).ready(function() {
		
		$( "#editPaymentConfig\\.nextExcuteTime" ).datepicker({

			changeMonth: true,
			changeYear: true,
			numberOfMonths: 1,
			dateFormat: 'yy/mm/dd',
			showButtonPanel: true,
			showOn: "both",
			buttonImage: "<c:url value='styles/default/images/calendar.gif'/>",
			buttonImageOnly: true,
			onClose: function(dateText, inst) { 
				$("#editPaymentConfig\\.limitDays").change();				
			}

		});

		$("#editPaymentConfig\\.limitDays").change(function() {
			var limitDays = $("#editPaymentConfig\\.limitDays").val();
			limitDays = parseInt(limitDays, 10);
			if (isNaN(limitDays)){
				$("#editPaymentConfig\\.payDue").val('');
				return false;
			}			
			var nextExcuteTime = $( "#editPaymentConfig\\.nextExcuteTime" ).datepicker("getDate");			
			nextExcuteTime.setDate(nextExcuteTime.getDate() + limitDays);
			$("#editPaymentConfig\\.payDue").val($.datepicker.formatDate($.datepicker._defaults.dateFormat, nextExcuteTime));
		});
		
		$("#editPaymentConfigDiv").dialog({
			title : '<%=UserUtil.getCurrentMenuItemName() %>',
			width : 600,
			resizable : false,
			modal : true
		});
		
		$("#canceleditPaymentConfigBtn").click(function() {
			$("#editPaymentConfigDiv").dialog("close");
		});
		
		$("#doSavePaymentConfigBtn").click(function() {
			var pk = $('#editPaymentConfig\\.paymentConfigPk').val();
			var groupNo = $('#editPaymentConfig\\.group\\.groupNo').val();
			var groupPk = $('#editPaymentConfig\\.group\\.groupPk').val();
			var configNo = $('#editPaymentConfig\\.configNo').val();
			var configName = $('#editPaymentConfig\\.configName').val();			
			//var configType = $('input[name=editPaymentConfig.configType]:checked').val();
			//var configType = $('#editPaymentConfig\\.configType').val();
			//var cycleType = $('input[name=editPaymentConfig.cycleType]:checked').val();
			var cycleType = $('#editPaymentConfig\\.cycleType').val();
			//var amount = $('#editPaymentConfig\\.amount').val();
			var offerAmount = $('#editPaymentConfig\\.offerAmount').val();
			var nextExcuteTime = $('#editPaymentConfig\\.nextExcuteTime').val();
			var configNote = $('#editPaymentConfig\\.configNote').val();
			var payDue = $('#editPaymentConfig\\.payDue').val();
			var limitDays = $('#editPaymentConfig\\.limitDays').val();
			
			<c:forEach items="${actionBean.editPaymentConfig.paymentConfigItems}" var="viewObj" varStatus="rowstat">
			var paymentConfigItemPk<c:out value='${rowstat.index}'/> = $('#editPaymentConfig\\.paymentConfigItems\\[<c:out value="${rowstat.index}" />\\]\\.paymentConfigItemPk').val();
			var itemName<c:out value='${rowstat.index}' /> = $('#editPaymentConfig\\.paymentConfigItems\\[<c:out value="${rowstat.index}" />\\]\\.itemName').val();
			var amount<c:out value='${rowstat.index}' /> = $('#editPaymentConfig\\.paymentConfigItems\\[<c:out value="${rowstat.index}" />\\]\\.amount').val();
			var configType<c:out value='${rowstat.index}' /> = $('#editPaymentConfig\\.paymentConfigItems\\[<c:out value="${rowstat.index}" />\\]\\.configType').val();
			</c:forEach>
			
			
			
			var errMsg = "";
			if (null == groupNo || 0 == groupNo.length){
				errMsg = errMsg + "業者代號 是必要欄位<br/>";
			}			
			
			if (null == configNo || 0 == configNo.length){
				errMsg = errMsg + "收費代碼 是必要欄位<br/>";
			}			
			
			if (null == configName || 0 == configName.length){
				errMsg = errMsg + "收費名稱 是必要欄位<br/>";
			}
			
			//if (null == configType || 0 == configType.length){
			//	errMsg = errMsg + "收費型式 是必要欄位<br/>";
			//}
			
			if (null == cycleType || 0 == cycleType.length){
				errMsg = errMsg + "收費週期 是必要欄位<br/>";
			}
			
			//if (null == amount || 0 == amount.length){
			//	errMsg = errMsg + "金額 是必要欄位<br/>";
			//}			
			
			<c:forEach items="${actionBean.editPaymentConfig.paymentConfigItems}" var="viewObj" varStatus="rowstat">
			if (null != configType<c:out value="${rowstat.index}" /> && 0 != configType<c:out value="${rowstat.index}" />.length){
				
				if (null == itemName<c:out value="${rowstat.index}" /> || 0 == itemName<c:out value="${rowstat.index}" />.length){
					errMsg = errMsg + "繳費項目 <c:out value='${rowstat.index+1}' /> 是必要欄位<br/>";
				}
				
				if (null == amount<c:out value="${rowstat.index}" /> || 0 == amount<c:out value="${rowstat.index}" />.length){
					errMsg = errMsg + "金額 <c:out value='${rowstat.index+1}' /> 是必要欄位<br/>";
				}
				else{
					var tmpInt = parseInt(amount<c:out value="${rowstat.index}" />, 10);
					if (isNaN(tmpInt)){
						errMsg = errMsg + "金額 <c:out value='${rowstat.index+1}' /> 應為整數<br/>";
					}
				}
			}
			
			</c:forEach>
			
			if (null == nextExcuteTime || 0 == nextExcuteTime.length){
				errMsg = errMsg + "下次執行日期 是必要欄位<br/>";
			}
			
			if (null == limitDays || 0 == limitDays.length){
				errMsg = errMsg + "繳費截止日 是必要欄位<br/>";
			}
			
			if (0 != errMsg.length){
				$("#ajaxErrorMessageDiv").html(errMsg);
				$("#ajaxErrorMessageDiv" ).dialog({  
					title:'請修正下列錯誤' 
					,width: 300
					, resizable: false
					, modal: true
					, buttons: {
						Ok: function() {
							$( this ).dialog( "close" );
						}
					}
				});
				return false;
			}
			
			
			$('#hidden\\.editPaymentConfig\\.paymentConfigPk').val(pk);
			$('#hidden\\.editPaymentConfig\\.group\\.groupNo').val(groupNo);
			$('#hidden\\.editPaymentConfig\\.group\\.groupPk').val(groupPk);
			$('#hidden\\.editPaymentConfig\\.configNo').val(configNo);
			$('#hidden\\.editPaymentConfig\\.configName').val(configName);
			$('#hidden\\.editPaymentConfig\\.cycleType').val(cycleType);
			$('#hidden\\.editPaymentConfig\\.offerAmount').val(offerAmount);
			$('#hidden\\.editPaymentConfig\\.nextExcuteTime').val(nextExcuteTime);
			$('#hidden\\.editPaymentConfig\\.configNote').val(configNote);
			$('#hidden\\.editPaymentConfig\\.limitDays').val(limitDays);
			$('#hidden\\.editPaymentConfig\\.payDue').val(payDue);
			
			<c:forEach items="${actionBean.editPaymentConfig.paymentConfigItems}" var="viewObj" varStatus="rowstat">
			$('#hidden\\.editPaymentConfig\\.paymentConfigItems\\[<c:out value="${rowstat.index}" />\\]\\.paymentConfigItemPk').val(paymentConfigItemPk<c:out value="${rowstat.index}" />);
			$('#hidden\\.editPaymentConfig\\.paymentConfigItems\\[<c:out value="${rowstat.index}" />\\]\\.itemName').val(itemName<c:out value="${rowstat.index}" />);			
			$('#hidden\\.editPaymentConfig\\.paymentConfigItems\\[<c:out value="${rowstat.index}" />\\]\\.configType').val(configType<c:out value="${rowstat.index}" />);
			$('#hidden\\.editPaymentConfig\\.paymentConfigItems\\[<c:out value="${rowstat.index}" />\\]\\.amount').val(amount<c:out value="${rowstat.index}" />);
			
			</c:forEach>
			
			doAjaxSavePaymentConfig();
		});
		
		function doAjaxSavePaymentConfig() {
			$('#doAjaxMethod').attr('name', 'doAjaxSavePaymentConfig');
						
			waitingDialog({});
			$.ajax({
				url : "<c:url value='/ajax/Soez1019.action'/>",
				async : true,
				type : 'POST',
				data : 	$("#editPaymentConfigHiddenForm").serialize(),
				dataType : 'html',
				success : function(data, textStatus, XMLHttpRequest) {
					$("#ajaxErrorMessageDiv").html(data);
					$("#ajaxErrorMessageDiv").dialog({
						title : '<%=UserUtil.getCurrentMenuItemName() %>',
						width : 300,
						resizable : false,
						modal : true,
						buttons : {
							Ok : function() {
								$(this).dialog("close");
								
								if (1==$("#doAjaxSavePaymentConfigResultCode").val()){
									doQueryAction();
									$("#editPaymentConfigDiv").dialog("close");
								}
							}
						}
					});
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
					$("#ajaxErrorMessageDiv").dialog({
						title : '系統錯誤',
						width : 600,
						resizable : false,
						modal : true,
						buttons : {
							Ok : function() {
								$(this).dialog("close");
							}
						}
					});
				},
				complete : function(XMLHttpRequest, textStatus) {
					closeWaitingDialog();
				}
			});
		}
		

		$('#findGroupNoImg1').click(function() {
			$('#hidden\\.queryGroupTrigger').val('2');
			$('#hidden\\.qryGroup\\.groupNo').val('');
			$('#hidden\\.qryGroup\\.groupName').val('');
			$('#qryGroup\\.groupNo').val('');
			$('#qryGroup\\.groupName').val('');
			$("#queryGroupResultDiv").html('');
			
			$("#findGroupDiv" ).dialog( "destroy" );
			$("#findGroupDiv" ).dialog({  
				title:'帳單業者查詢' 
				, width: 600
				, resizable: true
				, modal: true
			});
		});
		

		$("#doAjaxDeleteBtn").click(function() {
			
			var answer = confirm('是否確定刪除 ?');            	
        	if (!answer)
        		return false;
        	
			var paymentConfigPk = $('#hidden\\.editPaymentConfig\\.paymentConfigPk').val();
							
			waitingDialog({});
			$.ajax({
				url : "<c:url value='/ajax/Soez1019.action'/>",
				async : true,
				type : 'POST',
				data : {doDeletePaymentConfig: 'doDeletePaymentConfig', 'editPaymentConfig.paymentConfigPk': paymentConfigPk},
				dataType : 'html',
				success : function(data, textStatus, XMLHttpRequest) {						
					$("#ajaxErrorMessageDiv").html(data);
					$("#ajaxErrorMessageDiv").dialog({
						title : '<%=UserUtil.getCurrentMenuItemName() %>',
						width : 300,
						resizable : false,
						modal : true,
						buttons : {
							Ok : function() {								
								if (1==$("#doAjaxDeletePaymentConfigResultCode").val()){
									doQueryAction();
								}
								$(this).dialog("close");
								$("#editPaymentConfigDiv").dialog("close");
							}
						}
					});
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
					$("#ajaxErrorMessageDiv").dialog({
						title : '系統錯誤',
						width : 600,
						resizable : false,
						modal : true,
						buttons : {
							Ok : function() {
								$(this).dialog("close");
							}
						}
					});
				},
				complete : function(XMLHttpRequest, textStatus) {
					closeWaitingDialog();
				}
			});
		});
		
		$('#printBtn3').click(function() {
			divPrint('editPaymentConfigFormDiv');
		});
		
	});
//-->
</script>
<div id="editPaymentConfigFormDiv">
	<stripes:form id="editPaymentConfigFrom" name="editPaymentConfigFrom" 
		beanclass="com.feib.soeasy.action.Soez1019ActionBean" method="post" onsubmit="javascript: return false;">
	
	<table id="queryForm">
		<tr>
			<td colspan="4" style="color: #990000">
				<strong>
					<c:choose>
						<c:when test="${null == actionBean.editPaymentConfig.paymentConfigPk }">繳費資料件新增</c:when>
						<c:otherwise>繳費資料修改</c:otherwise>
					</c:choose> 
				</strong>
			</td>
			<stripes:hidden id="editPaymentConfig.paymentConfigPk" name="editPaymentConfig.paymentConfigPk" />
		</tr>
		
		<%if ( UserUtil.isBankGroup()){ %>
		<tr>
			<th>業者代號<span id="mustKeyInStar">**</span></th>
			<td>
				<c:choose>
					<c:when test="${null == actionBean.editPaymentConfig.paymentConfigPk }">
						<div id="findGroupNoImg1"><a href="javascript: void(0)" class="btnZoom">查詢</a></div>
						<stripes:text id="editPaymentConfig.group.groupNo" name="editPaymentConfig.group.groupNo" size="12" maxlength="12" readonly="true"/>
					</c:when>
					<c:otherwise>
						<c:out value="${actionBean.editPaymentConfig.group.groupNo }"/>
						<stripes:hidden id="editPaymentConfig.group.groupNo" name="editPaymentConfig.group.groupNo" />
					</c:otherwise>
				</c:choose>
			</td>
            <th>業者名稱</th>
            <td>
            	<span id="editPaymentConfig.group.groupName.span"><c:out value="${actionBean.editPaymentConfig.group.groupName }"/></span>                               
            </td> 
		</tr>
		
		<%} else {%>
		    <stripes:hidden id="editPaymentConfig.group.groupNo" name="editPaymentConfig.group.groupNo" />
		<%} %>	
		
		<tr>
			<th>收費代碼<span id="mustKeyInStar">**</span></th>
			<td>
				<c:choose>
					<c:when test="${null == actionBean.editPaymentConfig.paymentConfigPk }">
						<stripes:text id="editPaymentConfig.configNo" name="editPaymentConfig.configNo" size="10" maxlength="10"/>
					</c:when>
					<c:otherwise>
						<c:out value="${actionBean.editPaymentConfig.configNo }"/>
						<stripes:hidden id="editPaymentConfig.configNo" name="editPaymentConfig.configNo" />
					</c:otherwise>
				</c:choose>
				
			</td>
			<th>收費名稱<span id="mustKeyInStar">**</span></th>
			<td>
				<stripes:text id="editPaymentConfig.configName" name="editPaymentConfig.configName" size="10" maxlength="10"/>
			</td>
		</tr>
		<tr>
			<th>收費週期<span id="mustKeyInStar">**</span></th>
			<td>
				<stripes:select id="editPaymentConfig.cycleType" name="editPaymentConfig.cycleType">
					<stripes:option value="1" label="每月"/>
					<stripes:option value="2" label="每2個月"/>
					<stripes:option value="3" label="每季"/>
					<stripes:option value="4" label="每半年"/>
					<stripes:option value="5" label="每年"/>
				</stripes:select>
			</td>
			<th>優惠金額</th>
			<td>
				<stripes:text id="editPaymentConfig.offerAmount" name="editPaymentConfig.offerAmount" size="5" maxlength="6"/>
			</td>
		</tr>
		<c:forEach items="${actionBean.editPaymentConfig.paymentConfigItems}" var="viewObj" varStatus="rowstat">
		<tr>
			<th>繳費項目 ${rowstat.index+1}</th>
			<td>
				<stripes:hidden id="editPaymentConfig.paymentConfigItems[${rowstat.index}].paymentConfigItemPk" name="editPaymentConfig.paymentConfigItems[${rowstat.index}].paymentConfigItemPk"/>
				<stripes:text id="editPaymentConfig.paymentConfigItems[${rowstat.index}].itemName" name="editPaymentConfig.paymentConfigItems[${rowstat.index}].itemName" size="10" maxlength="10"/>
			</td>
			<th>金額 ${rowstat.index+1}</th>
			<td>
				<stripes:text id="editPaymentConfig.paymentConfigItems[${rowstat.index}].amount" name="editPaymentConfig.paymentConfigItems[${rowstat.index}].amount" size="5" maxlength="6"/>&nbsp;
				<stripes:select id="editPaymentConfig.paymentConfigItems[${rowstat.index}].configType" name="editPaymentConfig.paymentConfigItems[${rowstat.index}].configType">
					<stripes:option value="" label="-- 請選擇 --"/>
					<stripes:option value="1" label="依坪數"/>
					<stripes:option value="2" label="依車位數"/>
					<stripes:option value="3" label="固定金額"/>
				</stripes:select>
			</td>
		</tr>
		</c:forEach>
		<tr>
			<th>下次執行日期<span id="mustKeyInStar">**</span></th>
			<td>
				<stripes:text id="editPaymentConfig.nextExcuteTime" name="editPaymentConfig.nextExcuteTime" size="10" maxlength="10" formatPattern="yyyy/MM/dd" readonly="true"/>
			</td>
			<th>繳款截止日<span id="mustKeyInStar">**</span></th>
			<td>
				<stripes:text id="editPaymentConfig.limitDays" name="editPaymentConfig.limitDays" size="3" maxlength="3" />
				<stripes:text id="editPaymentConfig.payDue" name="editPaymentConfig.payDue" size="10" maxlength="10" formatPattern="yyyy/MM/dd" readonly="true"/>
			</td>
		</tr>
		
		<tr>
			<th>備註</th>
			<td colspan="3">
				<stripes:text id="editPaymentConfig.configNote" name="editPaymentConfig.configNote" size="40" maxlength="20"/>
			</td>
		</tr>
		
		<tr>
			<th>建立者</th>
			<td><c:out value="${actionBean.editPaymentConfig.createdBy }"/></td>
			<th>建立時間</th>
			<td><fmt:formatDate value="${actionBean.editPaymentConfig.createdDate}" pattern="yyyy/MM/dd HH:mm:ss"/></td>
		</tr>
		<tr>
			<th>修改者</th>
			<td><c:out value="${actionBean.editPaymentConfig.updatedBy }"/></td>
			<th>修改時間</th>
			<td><fmt:formatDate value="${actionBean.editPaymentConfig.updatedDate}" pattern="yyyy/MM/dd HH:mm:ss"/></td>
		</tr>
		<tr>
			<td colspan="4" class="queryFunction">
				<input type="button" id="doSavePaymentConfigBtn" value="確認" />
<c:if test="${null != actionBean.editPaymentConfig.paymentConfigPk }">
				<input type="button" id="doAjaxDeleteBtn" value="刪除" />
</c:if>
				<input type="button" id="printBtn3" value="列印" />
				<input type="button" id="canceleditPaymentConfigBtn" value="離開" />
			</td>
		</tr>
	</table>
</stripes:form>
</div>

<div id="editPaymentConfigHiddenDiv" style="display: none;">
<stripes:form id="editPaymentConfigHiddenForm" name="editPaymentConfigHiddenForm" 
	beanclass="com.feib.soeasy.action.Soez1019ActionBean" method="post" onsubmit="javascript: return false;">
	<stripes:hidden id="hidden.editPaymentConfig.paymentConfigPk" name="editPaymentConfig.paymentConfigPk" />
	<stripes:hidden id="hidden.editPaymentConfig.group.groupPk" name="editPaymentConfig.group.groupPk" />
	<stripes:hidden id="hidden.editPaymentConfig.group.groupNo" name="editPaymentConfig.group.groupNo" />
	<stripes:hidden id="hidden.editPaymentConfig.configNo" name="editPaymentConfig.configNo" />
	<stripes:hidden id="hidden.editPaymentConfig.configName" name="editPaymentConfig.configName" />
	
	<stripes:hidden id="hidden.editPaymentConfig.cycleType" name="editPaymentConfig.cycleType" />
	<stripes:hidden id="hidden.editPaymentConfig.offerAmount" name="editPaymentConfig.offerAmount" />
	<stripes:hidden id="hidden.editPaymentConfig.nextExcuteTime" name="editPaymentConfig.nextExcuteTime" />
	<stripes:hidden id="hidden.editPaymentConfig.payDue" name="editPaymentConfig.payDue" />
	<stripes:hidden id="hidden.editPaymentConfig.limitDays" name="editPaymentConfig.limitDays" />
	<stripes:hidden id="hidden.editPaymentConfig.configNote" name="editPaymentConfig.configNote" />
	
	<c:forEach items="${actionBean.editPaymentConfig.paymentConfigItems}" var="viewObj" varStatus="rowstat">
		<stripes:hidden id="hidden.editPaymentConfig.paymentConfigItems[${rowstat.index}].paymentConfigItemPk" name="editPaymentConfig.paymentConfigItems[${rowstat.index}].paymentConfigItemPk" />
		<stripes:hidden id="hidden.editPaymentConfig.paymentConfigItems[${rowstat.index}].itemName" name="editPaymentConfig.paymentConfigItems[${rowstat.index}].itemName"  />
		<stripes:hidden id="hidden.editPaymentConfig.paymentConfigItems[${rowstat.index}].amount" name="editPaymentConfig.paymentConfigItems[${rowstat.index}].amount"  />
		<stripes:hidden id="hidden.editPaymentConfig.paymentConfigItems[${rowstat.index}].configType" name="editPaymentConfig.paymentConfigItems[${rowstat.index}].configType" />
	</c:forEach>	
	<input type="hidden" id="doAjaxMethod" name="doAjaxSavePaymentConfig" />
</stripes:form>
</div>

